package chapter12;

import chapter11.Role;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.StatementCallback;

import java.sql.ResultSet;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-04-16 下午4:18
 */
public class TestDemo {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
        JdbcTemplate template = ctx.getBean("jdbcTemplate", JdbcTemplate.class);
        String sql = "SELECT id, name, note FROM role where id=1";
        Role role = template.queryForObject(sql, (resultSet, i) -> {
            Role r = new Role();
            r.setRoleId(resultSet.getLong("id"));
            r.setRoleName(resultSet.getString("name"));
            r.setNote(resultSet.getString("note"));
            return r;
        });
        System.out.println(role.toString());
        Role r2 = template.execute((StatementCallback<Role>) statement -> {
            Role r = new Role();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                r.setRoleId(rs.getLong("id"));
                r.setRoleName(rs.getString("name"));
                r.setNote(rs.getString("note"));
            }
            return r;
        });

        System.out.println(r2.toString());
    }
}
